<?php

namespace App\Http\Comm;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CommonController extends BaseController
{
	/**
	 * 把二维数组转化为树状结构数组
	 * 
	 * @author 孙丽男
	 * @DateTime 2018-06-05
	 * @param   array    $arr          要处理的数组
	 * @param   string   $pid          父级ID的值，默认是0
	 * @param   string   $idName       id的名称,默认是id
	 * @param   string   $pidName      pid的名称,默认是pid
	 * @param   string   $childName    下级的key名称，默认是child
	 * @return  array    $tree          生成的树状数组
	 */
	// (arr ,10000,code,parentCode,child)
	public function createTree($arr = array(), $pid = 0, $idName = 'id', $pidName = 'pid', $childName = 'child')
	{
	    $tree = array();
	    foreach($arr as $k => $v)
	    {
	        if($v[$pidName] == $pid)
	        {
	            $tmp = $arr[$k];
	            unset($arr[$k]);
	            $tmp[$childName] = $this->createTree($arr,$v[$idName],$idName,$pidName,$childName);
	            $tree[] = $tmp;
	        }
	    }
	    foreach ($tree as $key => $value) {
	    	
	    	if (empty($value['child'])) {
	    		
	    		unset($tree[$key]['child']);
	    	}
	    }
	    
	    return $tree;
	}

	/**
	 * [isAdmin 判断是否是超级管理员信息]
	 * @Author   孙丽男
	 * @DateTime 2018-06-06
	 * @param    [值]  int $value
	 * @param    [条件字段名]  int $field
	 * @param    [查询字段名]  int $findField
	 * @param    [表名]  str  $table
	 * @version  6.2
	 * @return   boolean    [description]
	 */
	public function isAdmin($userId ,$roleId){

		$roleArr = '';
		$userArr = '';

		if ($userId) {
			$userObj = Db::table('user')
							->whereIn('id',$userId)
							->where('number','admin')
							->first();

			$userArr = json_decode(json_encode($userObj),true);
		}

		if ($roleId) {
			
			$roleObj = Db::table('roles')
						->whereIn('id',$roleId)
						->where('name','超级管理员')
						->first();
			$roleArr = json_decode(json_encode($roleObj),true);
		}
		
		if (!empty($roleArr) || !empty($userArr)) {

			return true;

		}else{

			return false;
		}
	}

	 /**
	 * [upload 上传图片]
	 * @Author   孙丽男
	 * @DateTime 2018-06-06
	 * @param    [文件实例对象] $file
	 * @param    [文件存储路径] $path
	 * @version  6.2n]
	 */
	public function upload($file,$path){

	    //判断文件是否有效
	    if ( $file->isValid()) { 

	    	//扩展名
	        $extension = $file->getClientOriginalExtension(); 
	        //重命名
	        $filename = md5(time().mt_rand(10, 99)) . "." . $extension;    
	        $file->move($path, $filename); //移动至指定目录
	        
	        $filePath = $path.$filename;

	        return $filePath;

        }else{
        	return false;
        }
	}
	/**
	 * [uploadMany 多图上传]
	 * @Author   孙丽男
	 * @DateTime 2018-06-06
	 * @param    [文件实例对象] $file
	 * @param    [文件存储路径] $path
	 * @version  6.2n]
	 */
	public function uploadMany($file,$path){

		$filePath =[];  // 定义空数组用来存放图片路径
		foreach ($file as $key => $value) {
		  // 判断图片上传中是否出错
		   if (!$value->isValid()) {
		      return false;
		   }
		    if(!empty($value)){//此处防止没有多文件上传的情况
				$allowed_extensions = ["png", "jpg", "gif"];
				if ($value->getClientOriginalExtension() && !in_array($value->getClientOriginalExtension(), $allowed_extensions)) {
				    return false;
				}
				$extension = $value->getClientOriginalExtension();   // 上传文件后缀
				$fileName = md5(time().mt_rand(10, 99)).'.'.$extension; // 重命名
				$value->move($path, $fileName); // 保存图片
				$filePath[] = $path.$fileName; 

		    }
		}
		// 返回上传图片路径，用于保存到数据库中
		return $filePath;

	}

}